﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Paragon.Logging
{
    public class ThreadContext : ICloneable
    {
        private readonly static LocalDataStoreSlot s_threadLocalSlot = System.Threading.Thread.AllocateDataSlot();

        public static ThreadContext CurrentContext { get; private set; }

        static ThreadContext()
        {
            CurrentContext = new ThreadContext();
        }

        public ThreadContext()
        {
        }

        public void Clear()
        {
            Dictionary<string, object> dictionary = GetProperties(false);
            if (dictionary != null)
            {
                dictionary.Clear();
            }
        }

        public object this[string index]
        {
            get
            {
                Dictionary<string, object> dictionary = GetProperties(false);
                if (dictionary != null)
                {
                    if (dictionary.ContainsKey(index))
                        return dictionary[index];
                }
                return null;
            }
            set
            {
                Dictionary<string, object> dictionary = GetProperties(true);
                if (dictionary != null)
                {
                    if (dictionary.ContainsKey(index))
                        dictionary[index] = value;
                    else
                        dictionary.Add(index, value);
                }
            }
        }

        private Dictionary<string, object> GetProperties(bool create)
        {
            Dictionary<string, object> properties = (Dictionary<string, object>)System.Threading.Thread.GetData(s_threadLocalSlot);
            if (properties == null && create)
            {
                properties = new Dictionary<string, object>();
                System.Threading.Thread.SetData(s_threadLocalSlot, properties);
            }

            return properties;
        }

        public void Add(string key, object value)
        {
            Dictionary<string, object> dictionary = GetProperties(true);
            if (dictionary != null)
            {
                if (!dictionary.ContainsKey(key))
                    dictionary.Add(key, value);
                else
                    dictionary[key] = value;
            }
        }

        public bool ContainsKey(string key)
        {
            Dictionary<string, object> dictionary = GetProperties(false);
            if (dictionary != null)
                return dictionary.ContainsKey(key);
            return false;
        }

        public ICollection<string> Keys
        {
            get
            {
                Dictionary<string, object> dictionary = GetProperties(false);
                if (dictionary != null)
                    return dictionary.Keys;
                return null;
            }
        }

        public void Remove(string index)
        {
            Dictionary<string, object> dictionary = GetProperties(false);
            if (dictionary != null)
            {
                dictionary.Remove(index);
            }
        }

        public bool TryGetValue(string key, out object value)
        {
            return ((value = this[key]) != null);
        }

        public ICollection<object> Values
        {
            get
            {
                Dictionary<string, object> dictionary = GetProperties(false);
                if (dictionary != null)
                {
                    return dictionary.Values;
                }
                return null;
            }
        }


        internal Exception CopyTo(IDictionary<string, object> properties)
        {
            try
            {
                Dictionary<string, object> dictionary = GetProperties(false);
                if (dictionary != null)
                {
                    foreach (string key in dictionary.Keys)
                    {
                        object value = dictionary[key];

                        // File Log Listener cannot handle null values for extended properties
                        if (value == null)
                            value = "NULL";

                        if (!properties.ContainsKey(key))
                            properties.Add(key, value);
                        else
                            properties[key] = value;
                    }
                }
                return null;
            }
            catch (Exception ex)
            {
                return ex;
            }
        }

        #region ICloneable Members

        public object Clone()
        {
            Dictionary<string, object> results = new Dictionary<string, object>();

            foreach (string key in Keys)
            {
                results.Add(key, this[key]);
            }

            return results;
        }

        #endregion
    }
}
